<?php
class userController extends RPHP_Controller
{    

    public function checktao()
    {
        $m = $this->model;

        $user = $m->checkAuth();

        if ($user) {

            $url  = "/user/index";
            $data = $m->checkTao();
            if ($data['level_money'] > 0) {
                $url = "/user/tao?level_money=" . $data['level_money'];
            }

            $this->redirect($url);

        }
    }

    //晋升政策
    public function promote_policy()
    {

        $this->display();
    }

    //代理政策
    public function agency_policy()
    {

        $this->display();
    }

    //转账页面
    public function transfer()
    {
        $m = $this->model;

        $user = $m->checkAuth();

        if ($user) {
            $this->view->data = $m->getBalance();
            $this->display();
        }

    }
    public function choosetao()
    {
        $m = $this->model;

        $user = $m->checkAuth();

        if ($user) {

            $data = $m->checkTao();
            if ($data['level_money'] == $_GET['level_money']) {

                $this->view->level_money = $data['level_money'];
                $this->display();
            }

        }
    }

    public function tao()
    {
        $m = $this->model;

        $user = $m->checkAuth();

        if ($user) {

            $data = $m->checkTao();
            if ($data['level_money'] == $_GET['level_money']) {

                $this->view->level_money = $data['level_money'];
                $this->display();
            }

        }
    }

    public function index()
    {
        $m = $this->model;

        $user = $m->checkAuth();
        if ($user) {
        /*    $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
       echo $beginYesterday;die;*/
            $u = $m->getMemberInfo();

            if ($u['mobile'] == "") {
                $this->redirect("/user/update_account?type=mobile&a=m");
            } else {
                
                $this->view->member = $u;
                
                $this->view->user   = $user;
                $this->view->order  = $m->getOrderCountByType();

                // 获取直推数量
                $this->view->usernum = $m->getmemberCount();
                //查看是否有付款注册会员并且已发货
                $this->view->shipping = $m->getShippingStatus();
                $this->view->tao      = $m->checkTao();
                //签到信息
                $this->view->sign      = $m->getSignInDetail();
                //全局配置
                $this->view->config      = $m->getSetting();
                //print_r($this->view->config );die;
                $this->display();
            }

        }

    }

    //我的收藏

    public function collection()
    {
        $tpl  = 'collection_empty';
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }
            $data = $m->getCollectionList($page);
            if ($data['data']) {
                $tpl = '';
            }
            $this->view->data = $data;

            $this->display($tpl);
        }

    }

    //我的足迹
    public function footprint()
    {
        $tpl  = 'footprint_empty';
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }

            $data = $m->getFootList($page);

            if ($data['data']) {
                $tpl              = '';
                $this->view->data = $data;
            }

            $this->display($tpl);
        }
    }

    //开通支付
    public function open()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        $temp = "update_recode";
        if ($user) {
            $member = $m->getMemberInfo();
            $config = $m->getSetting();

            if ($config['vip_price'] > 0) {
                if ($member['pid'] > 0) {
                    $temp                  = "";
                    $this->view->vip_price = intval($config['vip_price']);
                    $this->view->user      = $member;

                }
                $this->display($temp);

            }
        }
    }

    //获取会员开通发货信息
    public function openshipping()
    {

        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $this->view->data = $m->getMemberOpenShippingDetail();
            $this->display();
        }

    }

    //保存开通会员
    public function save_member()
    {

        $m    = $this->model;
        $user = $m->checkAuth();

        if ($user) {

            $data = $m->createMemberOrder($_POST);
            if ($data['code'] == 200) {

                if ($data['paid']) {
                    $this->redirect("/user/checktao");
                } else {

                    if ($user['app'] == 1) {
                        $data['transaction_type'] = 2;
                    } else {
                        $data['transaction_type'] = 1;
                    }
                    $para['channel_id']     = $this->c['pay'][$data['transaction_type']]['channel_id'];
                    $para['out_trade_no']   = $data['order_id'];
                    $para['out_trade_type'] = "MEMBER";
                    $para['total_fee']      = $data['pay_fee'] * 100;
                    $para['timestamp']      = $_SERVER['REQUEST_TIME'];
                    ksort($para);
                    foreach ($para as $key => $value) {
                        $tmp[] = $key . "=" . $value;
                    }
                    $stringSignTemp = implode("&", $tmp) . "&channer_key=" . $this->c['pay'][$data['transaction_type']]['channer_key'];
                    $para['sign']   = strtoupper(md5($stringSignTemp));
                    $tmp[]          = "sign=" . $para['sign'];
                    $url            = $this->c['pay'][$data['transaction_type']]['gate_url'] . "?" . implode("&", $tmp);
                    $this->redirect($url);
                }

            } else {
                $this->redirect("/user/index?code=" . $data['code']);
            }
        }
    }

    //帐户管理
    public function account_manage()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $this->view->page_title = "帐户管理";

            $this->view->data   = $m->getUserProfileInfo();
            $user_info          = $m->getMemberInfo();
            $this->view->user   = $user_info;
            $this->view->cookie = $user;
            $this->display();
        }

    }

    public function update_account()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $type = $_GET['type'];
            $user = $m->getUserMore();
            if ($type == 'mobile') {

                $this->view->user = $user;

                $this->view->mobile = $user['mobile'];

                if ($_GET['a'] == "m") {
                    $this->view->page_title = "绑定手机号码";

                    $this->view->recode = $_SESSION['qrcode_recode'];

                } else {
                    $this->view->page_title = "更改手机号码";
                }

                $this->display("update_mobile");

            } elseif ($type == 'addr') {

                $this->view->page_title = "更改地区";
                $this->display("update_addr");
            } elseif ($type == 'about') {

                $this->view->page_title = "关于我们";
                $this->display("account_about");
            } elseif ($type == 'headimg') {
                $user_info = $m->getUserAccountInfo();
                if ($user_info['headimgurl'] > 0) {
                    $this->view->docs = $m->getDocs($user_info['headimgurl']);
                }

                $this->view->user       = $user_info;
                $this->view->page_title = "修改头像";
                $this->display("change_headimg");
            } elseif ($type == 'name') {
                $users                  = $m->getUserAccountInfo();
                $this->view->users      = $users;
                $this->view->page_title = "更改姓名";
                $this->display("update_name");
            }

        }
    }

    //修改个人地区
    public function update_info()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $this->view->page_title = "更改地区";
            $this->view->province   = $m->getRegion();
            $data                   = $m->getUserProfileInfo();
            if ($data['info']['province_id'] > 0) {
                $data['city_list'] = $m->getRegion($data['info']['province_id']);
            }

            if ($data['info']['city_id'] > 0) {
                $data['county_list'] = $m->getRegion($data['info']['city_id']);
            }

            $this->view->data = $data;
            $this->display();
        }

    }

    public function ajax()
    {
        $m    = $this->model;
        $user = $m->checkAuth(false);
        if ($user) {

            $data = array('code' => 400, "msg" => "");

            if ($_POST['act'] == 'region') {
                $data_r = $m->getRegion($_POST['parent_id']);
                if ($data_r) {
                    $data = array('code' => 200, 'data' => $data_r);
                } else {
                    $data = array('code' => 404);
                }

            } elseif ($_POST['act'] == 'save_info') {
                //保存人个资料地区

                $data['code'] = $m->saveUserAccount($_POST);
            } elseif ($_POST['act'] == 'update_pwd') {
                //修改密码

                $data['code'] = $m->updateUserPwd($_POST);
            } elseif ($_POST['act'] == 'save_mobile') {

                $data['code'] = $m->updateUserMobile($_POST);
            } elseif ($_POST['act'] == 'change_headimg') {
                $data['code'] = $m->updateUserHeadimg($_POST);
            } elseif ($_POST['act'] == 'change_logoimg') {
                $data['code'] = $m->updateShopLogo($_POST['headimgurl']);
            } elseif ($_POST['act'] == 'save_shopinfo') {
                $data['code'] = $m->updateShopInfoField($_POST);
            } elseif ($_POST['act'] == 'upload_image_from_ajax') {
                //上传图片

                $data = $m->saveDataBase64Image($_POST['img_data']);
            } elseif ($_POST['act'] == 'change_selfdomain') {
                $data['code'] = $m->updateShopInfoField($_POST);
            } elseif ($_POST['act'] == 'change_wechatpublic') {
                $data['code'] = $m->updateShopInfoField($_POST);
            } elseif ($_POST['act'] == 'change_memberitem') {
                $data['code'] = $m->updateShopInfoField($_POST);
            } elseif ($_POST['act'] == 'is_code') {
                $data = $m->isCode($_POST);
            } elseif ($_POST['act'] == 'footp') {
                $data = $m->delFoots($_POST);
            } elseif ($_POST['act'] == 'collect') {
                $data = $m->delCollect($_POST);
            }            
            // 获取短信验证码
            elseif ($_POST['act'] == 'sendsmscode') {

                $data = $m->sendSMScode($_POST);

            }
            // 获取更多足迹
            elseif ($_POST['act'] == "getfootprint") {
                $data = $m->getFootList(intval($_POST['page']));
            }
            // 获取更多收藏
            elseif ($_POST['act'] == "getcollection") {
                $data = $m->getCollectionList(intval($_POST['page']));
            }
            // 移除单个收藏
            elseif ($_POST['act'] == "removecollection") {
                $data = $m->removeCollection(intval($_POST['id']));
            } //修改姓名
            elseif ($_POST['act'] == "name") {

                $data = $m->saveUserName($_POST);
            } //确认收货
            elseif ($_POST['act'] == "receive_order") {

                $data = $m->reveiveOrder($_POST['order_id']);
            } //修改pid
            elseif ($_POST['act'] == "save_code") {

                $data = $m->saveMemberCode($_POST['code']);
            } elseif ($_POST['act'] == "sub_transfer") {
                //转账功能
                $data = $m->subTransfer($_POST);
            } elseif ($_POST['act'] == "save_upgrade") {
                //代理升级申请

                $data = $m->subUpgrade($_POST);
            } elseif ($_POST['act'] == "save_sign") {
                //签到处理

                $data = $m->saveSign($_POST);
            }elseif ($_POST['act'] == "get_name") {
                //查找转账人资料
                $data = $m->getMemberName($_POST);
            }

            echo json_encode($data);

        }
    }

    //收货地址
    public function addrList()
    {

        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            $this->view->user = $user;
            $param['uid']     = $user['uid'];
            $data             = $m->list_user_address($param);
            $this->view->data = $data;

            //$this->view->cate = $m->getCategory();
            $this->view->page_title = "地址管理";
            $this->display();
        }
    }

    //收货地址 异步分页
    public function addr_ajax()
    {

        $m = $this->model;
        //        $shop = $m->checkShop(false);
        $user = $m->checkAuth(false);
        if ($user) {
            $param['uid'] = $user['uid'];
            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }

            $data = $m->list_user_address($param, $page);
            echo json_encode($data);
        }
    }

    public function ajax_set_address()
    {
        $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
        do {
            if (!$id > 0) {
                $res['success'] = -1;
                break;
            }

            $m    = $this->model;
            $user = $m->checkAuth();

            if (!$user['uid'] > 0) {
                $res['success'] = -1;
                break;
            }

            $m->set_user_address($id, $user['uid']);

            $res['success'] = 1;
        } while (false);

        echo exit(json_encode($res));
    }

    // 保存收货地址
    public function ajax_save_address()
    {

        $code = 800;
        $m    = $this->model;

        //        $shop = $m->checkShop(false);
        $user = $m->checkAuth(false);
        if ($user) {

            $code = 900;

            if ($_POST['act'] == "save") {
                $code = $m->saveAddress($_POST);
            } elseif ($_POST['act'] == 'edit_addr') {
                $code = $m->updateAddress($_POST);

            }

        }

        echo $code;
    }

    //添加收货地址
    public function add_useraddr()
    {
        $m = $this->model;

        //        $shop = $m->checkShop();
        $user = $m->checkAuth();
        if ($user) {
            $data = $m->getRegionNew();
            if ($data[0]) {
                foreach ($data[0] as $region_id => $r) {
                    $region[] = array('name' => $r['region_name'], 'id' => $region_id);

                    if ($data[$region_id]) {
                        $fi = count($region) - 1;
                        foreach ($data[$region_id] as $sec_id => $s) {
                            $region[$fi]['sub'][] = array('name' => $s['region_name'], 'id' => $sec_id);

                            if ($data[$sec_id]) {
                                $si = count($region[$fi]['sub']) - 1;
                                foreach ($data[$sec_id] as $thr_id => $t) {
                                    $region[$fi]['sub'][$si]['sub'][] = array('name' => $t['region_name'], 'id' => $thr_id);
                                }
                            }
                        }
                    }
                }
            }
            $this->view->region = json_encode($region);
            $this->display();
        }

    }

    //编辑收货地址
    public function edit_user_address()
    {
        $m = $this->model;

        //        $shop = $m->checkShop();
        $user = $m->checkAuth();
        if ($user) {
            $id      = intval($_GET['id']);
            $cartIds = trim($_GET['cartIds']);
            $pid     = intval($_GET['pid']);
            if ($id) {
                $data                 = $m->getAddrInfo($id);
                $this->view->data     = $data;
                $this->view->cartIds  = $cartIds;
                $this->view->pid      = $pid;
                $this->view->province = $m->getRegion();
                $this->view->city     = $m->getRegion($data['info']['province_id']);
                $this->view->country  = $m->getRegion($data['info']['city_id']);
                $data                 = $m->getRegionNew();
                if ($data[0]) {
                    foreach ($data[0] as $region_id => $r) {
                        $region[] = array('name' => $r['region_name'], 'id' => $region_id);

                        if ($data[$region_id]) {
                            $fi = count($region) - 1;
                            foreach ($data[$region_id] as $sec_id => $s) {
                                $region[$fi]['sub'][] = array('name' => $s['region_name'], 'id' => $sec_id);

                                if ($data[$sec_id]) {
                                    $si = count($region[$fi]['sub']) - 1;
                                    foreach ($data[$sec_id] as $thr_id => $t) {
                                        $region[$fi]['sub'][$si]['sub'][] = array('name' => $t['region_name'], 'id' => $thr_id);
                                    }
                                }
                            }
                        }
                    }
                }
                $this->view->region     = json_encode($region);
                $this->view->page_title = "编辑收货地址";
                $this->display();
            }

        }

    }

    //用户地址判断  不能超过3个
    public function number_ajax()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            if ($_POST['act'] == 'address_number') {
                //用户地址判断
                $data['code'] = $m->address_number();
                echo json_encode($data);
            }

        }

    }

    public function ajax_del_address()
    {
        $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
        do {
            if (!$id > 0) {
                $res['success'] = -1;
                break;
            }

            $m    = $this->model;
            $user = $m->checkAuth();

            if (!$user['uid'] > 0) {
                $res['success'] = -1;
                break;
            }

            $m->del_user_address($id, $user['uid']);

            $res['success'] = 1;
        } while (false);

        echo exit(json_encode($res));
    }

    //邀请有礼
    public function invite()
    {

        $pid = intval($_GET['uid']);
        if ($pid > 0) {
            $m    = $this->model;
            $user = $m->checkAuth(false);
            if ($user && $pid == $user['uid']) {

                $this->view->JSAPI = $m->getJSApiData();

                $this->view->info = $m->getMyProfile($user['uid'], false);

                $this->display();
            } else {
                $this->redirect("/wechat/oauth?pid=" . $pid);
            }
        }

    }
    //升级代理
    public function upgrade()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $uid         = intval($_GET['uid']);
            $member_info = $m->getMember($uid);
            if ($member_info['level_type'] == 2 && $member_info['agent_level'] < 4) {
                $this->view->data = $member_info;
                $this->display();
            }

        }

    }

    //我的推荐
    public function recommend()
    {
        $tpl  = 'recommend_empty';
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $para = array();
            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }

            if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }

            $data = $m->recommend($para, $page);
            if ($data) {
                $tpl = '';
                if ($_GET['act'] == "ajax") {

                    echo json_encode($data);die;
                }
            }

            $this->view->data       = $data;
            $this->view->page_title = "我的推荐";
            $this->display($tpl);
        }

    }

    //我的优惠卷
    public function coupon()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }
            $type = isset($_GET['type']) ? intval($_GET['type']) : 0;

            $data = $m->coupon($type, $page);

            if ($data) {

                if ($_GET['act'] == "ajax") {

                    echo json_encode($data);die;
                }
            }
            $this->view->coupon     = $m->getCouponNum();
            $this->view->data       = $data;
            $this->view->page_title = "我的优惠卷";
            $this->display();
        }

    }

    //查询我的推荐下级
    public function subordinate()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            if ($_GET['uid']) {
                $uid = intval($_GET['uid']);
            }
            if (!$_GET['page']) {
                $page = 1;
            } else {
                $page = intval($_GET['page']);
                if ($page < 1) {
                    $page = 1;
                }

            }

            if (!empty($_GET['keyword'])) {
                $para['keyword'] = addslashes(stripslashes(trim($_GET['keyword'])));
            }
            $data = $m->subordinate($uid, $para, $page);

            if ($_GET['act'] == "ajax") {

                echo json_encode($data);die;
            } else {
                $this->view->data       = $data;
                $this->view->page_title = "推荐下级";
                $this->display();

            }

        }

    }

    // 积分抽奖
    public function prizedraw()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            $this->view->balance = $m->getBalance();
            $this->display();
        }
    }

    //常见问题
    public function question()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $this->view->page_title = "常见问题";
            $this->display();
        }

    }

    //我的钱包
    public function my_bag()
    {

        $m = $this->model;

        $user = $m->checkAuth();
        if ($user) {
            $user             = $m->getShopMoney($user['shopid']);
            $this->view->user = $user;

            $this->view->page_title = "我的钱包";
            $this->display();
        }
    }

    // 协议页面
    public function xieyi()
    {
        $this->display();
    }

    // 我要成为县代理
    public function county_agent()
    {
        $this->display();
    }

    // 我要成为县代理（公告）
    public function county_agent_info()
    {
        $this->display();
    }

    //县代理统计
    public function county_agent_statis()
    {

        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $temp               = "";
            $member             = $m->getMember();
            $this->view->member = $member;
            //省县市配置
            $config = $m->getSetting();

            // print_r($config);die;
            //统计
            $data          = $m->getCountyAgencyStatistics();
            $my_direct_num = $m->getMyDirectNum();
            $data['team_num'] -= $my_direct_num;
            $data['my_direct_num'] = $my_direct_num;
            $level                 = 0;
            //if($data['my_direct_num'] >= $config['county_direct_num'] && $data['my_direct_num'] < $config['city_direct_num'] && $data['team_num'] >= $config['county_team_num'] && $data['team_num'] < $config['city_team_num'] && $data['team_money'] >= $config['county_team_perfor'] && $data['team_money'] < )

            if ($data['my_direct_num'] >= $config['province_direct_num'] && $data['team_num'] >= $config['province_team_num'] && $data['team_money'] >= $config['province_team_perfor']) {
                //省代理
                $level = 6;
            } else if ($data['my_direct_num'] >= $config['city_direct_num'] && $data['team_num'] >= $config['city_team_num'] && $data['team_money'] >= $config['city_team_perfor']) {
                //市代理
                $level = 5;
            } else if ($data['my_direct_num'] >= $config['county_direct_num'] && $data['team_num'] >= $config['county_team_num'] && $data['team_money'] >= $config['county_team_perfor']) {
                //县代理
                $level = 4;
            }

            if ($level == 4) {
                $temp = "city_agent_statis";
            } else if ($level == 5 || $level == 6) {
                $temp = "province_agent_statis";

            }
            $m->saveMemberLevel($level);
            $config['county_team_perfor']   = $config['county_team_perfor'] / 10000;
            $config['city_team_perfor']     = $config['city_team_perfor'] / 10000;
            $config['province_team_perfor'] = $config['province_team_perfor'] / 10000;
            $this->view->config             = $config;
            $this->view->data               = $data;
            $this->view->level              = $level;
            $this->display($temp);

        }

    }

    //生成我的推广二维码
    public function qrcode()
    {

        $m    = $this->model;
        $user = $m->checkAuth(false);
        if ($user) {

            $pro = $m->getProfile();

            $qrcode_url = "http://" . $_SERVER['HTTP_HOST'] . "/index/gateway?recode=" . $pro['recode'];

            $qrcode_bgpic = "./tmp/share.jpg";
            $qrcode_path  = './tmp/user' . $user['uid'] . '.png';

            RPHP::loadClass("phpqrcode", "extend");
            QRcode::png($qrcode_url, $qrcode_path, 'QR_ECLEVEL_L', 7, 2);

            // $this->createLogo($qrcode_path);

            //读取背景图片
            $img_out    = imagecreatefromjpeg($qrcode_bgpic);
            $img_qrcode = imagecreatefrompng($qrcode_path);
            $width      = $height      = 240;

            $rweixindata = $m->getRefreshWechatInfo();

            if ($rweixindata['nickname'] != "") {
                $pro['realname']             = $rweixindata['nickname'];
                $pro['wechat']['headimgurl'] = $rweixindata['headimgurl'];
            }

            //昵称处理
            if ($pro['realname'] != "") {
                $fontfile  = "style/font/FTLTLT.TTF";
                $textcolor = imagecolorallocate($img_out, 0, 0, 0);

                $max_w = 750;
                $len   = mb_strlen($pro['realname'], "utf-8");
                $per_w = 26;

                $fx = ceil(($max_w - $len * $per_w) / 2);

                imagettftext($img_out, 22, 0, $fx, 268, $textcolor, $fontfile, $pro['realname']);
            }
            
            //复制二维码到背景图片
            imagecopy($img_out, $img_qrcode, 266, 554, 0, 0, $width, $height);
            @unlink($qrcode_path);

            //保存头像到本地
            if ($pro['wechat']['headimgurl'] != "") {
                $h_imageinfo = $m->downloadimageformweixin($pro['wechat']['headimgurl']);
                if ($h_imageinfo != "") {
                    $head_path = './tmp/head_' . $user['uid'] . '.jpg';
                    $head_file = @fopen($head_path, 'w');
                    @fwrite($head_file, $h_imageinfo);
                    @fclose($head_file);

                    if (!file_exists($head_path)) {
                        $head_path = false;
                    }
                }
            }

            if ($head_path) {
                $width = $height = 130;
                $this->resizejpg($head_path, $width, $height);
                $img_head = imagecreatefromjpeg($head_path);
                //复制头像到背景图片
                imagecopy($img_out, $img_head, 310, 63, 0, 0, $width, $height);
                @unlink($head_path);
            }

            header("Content-type: image/jpeg");
            imagejpeg($img_out, null, 100);

        }
    }

    // 将二维码与LOGO组合
    public function createLogo($QR)
    {
        $qrcode_path    = $QR;
        $QR             = imagecreatefromstring(file_get_contents($QR));
        $logo           = imagecreatefromstring(file_get_contents("./tmp/logo.png"));
        $QR_width       = imagesx($QR); //二维码图片宽度
        $QR_height      = imagesy($QR); //二维码图片高度
        $logo_width     = imagesx($logo); //logo图片宽度
        $logo_height    = imagesy($logo); //logo图片高度
        $logo_qr_width  = $QR_width / 5;
        $scale          = $logo_width / $logo_qr_width;
        $logo_qr_height = $logo_height / $scale;
        $from_width     = ($QR_width - $logo_qr_width) / 2;
        //重新组合图片并调整大小
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);

        header("Content-type: image/png");
        imagepng($QR, $qrcode_path);
    }

    private function resizejpg($imgsrc, $imgWidth, $imgHeight, $quality = 100)
    {
        $arr = getimagesize($imgsrc);
        // header("Content-type: image/jpg");
        $img_source = imagecreatefromjpeg($imgsrc);
        $image      = imagecreatetruecolor($imgWidth, $imgHeight);
        imagecopyresampled($image, $img_source, 0, 0, 0, 0, $imgWidth, $imgHeight, $arr[0], $arr[1]);
        imagejpeg($image, $imgsrc, $quality);
        return $imgsrc;
    }

    //系统公告
    public function affiche()
    {
        $tpl  = 'affiche_empty';
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {

            $data = $m->affiche($user['level_type']);
            if ($data) {
                $tpl = '';
                if ($_GET['act'] == "ajax") {

                    echo json_encode($data);die;
                }
            }

            $this->view->data       = $data;
            $this->view->page_title = "系统公告";
            $this->display($tpl);
        }

    }

    //系统公告详细
    public function affiche_show()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            if ($_GET['id']) {
                $id = intval($_GET['id']);
            }
            $data                   = $m->affiche($id);
            $this->view->data       = $data;
            $this->view->page_title = "系统公告";
            $this->display();
        }

    }

    public function equity()
    {
        $m    = $this->model;
        $user = $m->checkAuth();
        if ($user) {
            $this->display();
        }
    }

}
